﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFIDA.U9.PM.PO;

namespace UFIDA.U9.Cust.YYD.PlugBE.PurchaseOrderP
{
    public static class POServices
    {
        public static WMSPODTO TransWMSDTO(PurchaseOrder holder)
        {
            List<POLine> POLines = holder.POLines.Cast<POLine>().ToList();
            WMSPODTO json = new WMSPODTO();
            json.header.type = "O";
            json.header.factoryCode = holder.POLines[0].DescFlexSegments.PubDescSeg2;
            json.header.supplierCode = holder.Supplier.Code;
            json.header.orderNo = holder.DocNo;
            if (holder.PurOper != null)
            {
                json.header.buyer = new Buyer
                {
                    code = holder.PurOper.Code,
                    name = holder.PurOper.Name,
                };
            }
            json.header.erpStatus = new Erpstatus
            {
                code = holder.Status.Value.ToString(),
                name = PublicMethod.GetSysEnumName(holder.Status)
            };
            json.header.subType = new Subtype
            {
                code = holder.DocumentType.Code,
                name = holder.DocumentType.Name
            };
            json.header.outsourceType = new Outsourcetype
            {
                code = holder.SubType?.Value.ToString(),
                name = PublicMethod.GetSysEnumName(holder.SubType)
            };
            json.header.businessDate = holder.BusinessDate.ToString();
            //json.header.deliverDate = ;
            if (holder.PurDept != null)
            {
                json.header.dept = new Dept
                {
                    code = holder.PurDept?.Code,
                    name = holder.PurDept?.Name,
                };
            }
            json.header.srcType = new Srctype
            {
                code = POLines[0].SrcDocType?.Value.ToString(),
                name = PublicMethod.GetSysEnumName(POLines[0].SrcDocType),
            };
            foreach (var line in POLines)
            {
                PODetail linedto = new PODetail();
                linedto.line = new POLineDto
                {
                    rcvWhCode = line.POShiplines[0]?.Wh.Code,
                    lineNo = line.DocLineNo.ToString(),
                    materialCode = line.ItemInfo.ItemCode,
                    demandQty = line.PurQtyTU,
                    unit = new Unit { code = line.TradeUOM?.Code, name = line.TradeUOM?.Name },
                    present = line.IsPresent,
                    srcType = new Srctype1 { code = line.SrcDocType?.Value.ToString(), name = PublicMethod.GetSysEnumName(line.SrcDocType) },
                    srcCode = line.SrcDocInfo.SrcDocNo,
                    factoryCode = line.DescFlexSegments.PubDescSeg2
                };
                //备料
                linedto.bom = new List<BomDto>();
                foreach (var pick in line.SCMPickHead.SCMPickListS)
                {
                    BomDto bomDto = new BomDto();
                    bomDto.issueWhCode = pick.SupplyWh?.Code;
                    bomDto.lineNo = pick.PickLineNo.ToString();
                    bomDto.materialCode = pick.ItemInfo.ItemCode;
                    bomDto.erpId = pick.ID.ToString();
                    bomDto.demandQty = pick.ActualReqQty;
                    bomDto.unit = new Unit1
                    {
                        code = pick.IssueUOM?.Code,
                        name = pick.IssueUOM?.Name
                    };
                    linedto.bom.Add(bomDto);
                }
                json.details.Add(linedto);
            }
            return json;
        }

        public static WMSPODTO TransWMSDTO(PurchaseOrder.EntityOriginal holder)
        {
            List<POLine> POLines = holder.POLines.Cast<POLine>().ToList();
            WMSPODTO json = new WMSPODTO();
            json.header.type = "O";
            json.header.factoryCode = holder.POLines[0].DescFlexSegments.PubDescSeg2;
            json.header.supplierCode = holder.Supplier.Code;
            json.header.orderNo = holder.DocNo;
            if (holder.PurOper != null)
            {
                json.header.buyer = new Buyer
                {
                    code = holder.PurOper.Code,
                    name = holder.PurOper.Name,
                };
            }
            json.header.erpStatus = new Erpstatus
            {
                code = holder.Status.Value.ToString(),
                name = PublicMethod.GetSysEnumName(holder.Status)
            };
            json.header.subType = new Subtype
            {
                code = holder.DocumentType.Code,
                name = holder.DocumentType.Name
            };
            json.header.outsourceType = new Outsourcetype
            {
                code = holder.SubType?.Value.ToString(),
                name = PublicMethod.GetSysEnumName(holder.SubType)
            };
            json.header.businessDate = holder.BusinessDate.ToString();
            //json.header.deliverDate = ;
            if (holder.PurDept != null)
            {
                json.header.dept = new Dept
                {
                    code = holder.PurDept?.Code,
                    name = holder.PurDept?.Name,
                };
            }
            json.header.srcType = new Srctype
            {
                code = POLines[0].SrcDocType?.Value.ToString(),
                name = PublicMethod.GetSysEnumName(POLines[0].SrcDocType),
            };
            foreach (var line in POLines)
            {
                PODetail linedto = new PODetail();
                linedto.line = new POLineDto
                {
                    rcvWhCode = line.POShiplines[0].Wh?.Code,
                    lineNo = line.DocLineNo.ToString(),
                    materialCode = line.ItemInfo.ItemCode,
                    demandQty = line.PurQtyTU,
                    unit = new Unit { code = line.TradeUOM?.Code, name = line.TradeUOM?.Name },
                    present = line.IsPresent,
                    srcType = new Srctype1 { code = line.SrcDocType?.Value.ToString(), name = PublicMethod.GetSysEnumName(line.SrcDocType) },
                    srcCode = line.SrcDocInfo.SrcDocNo,
                    factoryCode = line.DescFlexSegments.PubDescSeg2
                };
                //备料
                linedto.bom = new List<BomDto>();
                foreach (var pick in line.SCMPickHead.SCMPickListS)
                {
                    BomDto bomDto = new BomDto();
                    bomDto.issueWhCode = pick.SupplyWh?.Code;
                    bomDto.lineNo = pick.PickLineNo.ToString();
                    bomDto.materialCode = pick.ItemInfo.ItemCode;
                    bomDto.erpId = pick.ID.ToString();
                    bomDto.demandQty = pick.ActualReqQty;
                    bomDto.unit = new Unit1
                    {
                        code = pick.IssueUOM?.Code,
                        name = pick.IssueUOM?.Name
                    };
                    linedto.bom.Add(bomDto);
                }
                json.details.Add(linedto);
            }
            return json;
        }

        public static bool IsChange(PurchaseOrder.EntityOriginal ori, PurchaseOrder holder,out WMSPODTO json)
        {
            var oriJson = TransWMSDTO(ori);
            json = TransWMSDTO(holder);
            if (JsonConvert.SerializeObject(oriJson) != JsonConvert.SerializeObject(json))
                return true;
            else
                return false;
        }
    }
}
